<script setup>
import dayjs from 'dayjs';
import { ref, reactive, nextTick } from 'vue';
import Type from '@components/search/type';
import Search from '@components/search/index';
import PageWrapper from '@components/PageWrapper.vue';
import { common, service } from '@utils';
import TitleValue from '@components/TitleValue';
import { useRoute } from 'vue-router';
import { View } from '@element-plus/icons-vue';
import { usePremission } from '@hooks';

const route = useRoute();

let activeName = reactive('已分账流水');

const premission = usePremission({
    view: 'bfqz:order:view',
    export: 'bfqz:order:export',
});

let queryForm = reactive({
    time: [],
    state: '',
});

let end = new Date().getTime();
let start = new Date().getTime() - 3600 * 1000 * 24 * 6;
end = dayjs(end).format('YYYY-MM-DD 23:59:59');
start = dayjs(start).format('YYYY-MM-DD 00:00:00');
// end = dayjs(end).format('YYYY-MM-DD')
// start = dayjs(start).format('YYYY-MM-DD')
queryForm.time = [start, end];
let objectData = ref({});
let fund = ref({});
let searchs = reactive([
    {
        type: Type.DATE_RANGE,
        width: 6,
        key: 'time',
        default: queryForm.time,
        format: 'YYYY-MM-DD HH:mm:ss',
    },
    {
        type: Type.INPUT,
        key: 'orderId',
        width: 3,
        default: route.query.orderId || '',
        placeholder: '订单号',
    },
    {
        type: Type.INPUT,
        key: 'balanceNo',
        width: 3,
        placeholder: '交易流水号',
        valueKey: 'val',
        data: [],
    },
    {
        type: '',
        key: 'outTradeNo',
        width: 0.0001,
        placeholder: '支付流水号',
        valueKey: 'val',
        data: [],
    },
    {
        type: Type.SELECT,
        key: 'companyCode',
        width: 3,
        placeholder: '商户名',
        valueKey: 'companyCode',
    },
    {
        type: Type.SELECT,
        key: 'state',
        width: 3,
        placeholder: '订单状态',
        valueKey: 'value',
        data: queryForm.state,
        default: queryForm.state,
    },
    {
        width: 5,
        // type: Type.SEARCH_BUTTON
        // type: Type.SEARCH_EXPORT_BUTTON,
        type: Type.SEARCH_EXPORT_BUTTON,
    },
]);
const pageRef = ref();
const tableRef = ref();
const searchRef = ref();
const updateSearchRefKey = ref(new Date().getTime());
const numLine = ref(1);
const orderStateList = ref()
const onLineNum = num => {
    numLine.value = num;
};
const onSearch = param => {
    queryForm = param;
    pageRef.value.refresh();
};

const getData = async ({ pageNum, pageSize }, cb) => {
    const query = { ...queryForm };
    if (query.time && query.time.length > 0) {
        query.startCreateTime = query.time[0];
        query.endCreateTime = query.time[1];
        // query.state = ""
    }
    delete query.time;
    if (activeName == '已分账流水') {
        query.type = '1';
        searchs[5].data = searchs[5].yesData;
        // fund.value = balanceAmount
    } else if (activeName == '未分账流水') {
        query.type = '0';
        searchs[5].data = searchs[5].noData;
    } else if (activeName == '押金流水') {
        query.type = '2';
    } else if (activeName == '退款流水') {
        query.type = '3';
    }
    const res = await service.fund.fashionable(pageNum, pageSize, query);
    cb(res.page);
    objectData.value = res;
    setTimeout(() => {
        tableRef.value.scrollTo(0, 0);
    }, 50);
};

const getStatuss = async query => {
    const res = await service.auth.sysdirectListbyCode({ code: 'financialOrdeState,noFinancialOrdeState,orderState' });
    if (res) {
        let statuss = [];
        let noStatuss = [];
        let orderState = []
        res.forEach(item => {
            switch (item.typeCode) {
                case 'financialOrdeState':
                    statuss.push(item);
                    break;
                case 'noFinancialOrdeState':
                    noStatuss.push(item);
                    break;
                case 'orderState':
                    orderState.push(item);
                    break;
                default:
                    break;
            }
        });
        searchs[5].data = statuss;
        searchs[5].yesData = statuss;
        searchs[5].noData = noStatuss;
        orderStateList.value = orderState;
    }
    const company = await service.auth.companyAll();
    searchs[4].data = company;
};
// nextTick(()=>{
getStatuss();
// })

const handleClick = (tab, event) => {
    activeName = tab.props.label;
    if (activeName == '未分账流水' || activeName == '已分账流水') {
        searchs[5].type = 'SELECT';
        searchs[4].type = 'SELECT';
        searchs[5].width = 3;
        searchs[4].width = 3;
    } else {
        searchs[5].type = null;
        searchs[4].type = null;
        searchs[4].width = 0.0001;
        searchs[5].width = 0.0001;
    }
    if (activeName == '已分账流水' || activeName == '押金流水') {
        searchs[3].type = null;
        searchs[3].width = 0.0001;
        searchs[2].type = 'INPUT';
        searchs[2].width = 3;
    } else {
        searchs[2].type = null;
        searchs[2].width = 0.0001;
        searchs[3].type = 'INPUT';
        searchs[3].width = 3;
    }
    // onSearch(queryForm);
    updateSearchRefKey.value = new Date().getTime();
    searchRef.value.reset();
};

const exportData = async query => {
    query = { ...query };
    if (query.time && query.time.length > 0) {
        query.startCreateTime = query.time[0];
        query.endCreateTime = query.time[1];
        delete query.time;
    }
    
    if (activeName == '已分账流水') {
        query.fileName = '已分账流水.xlsx';
        query.type = '1';
    } else if (activeName == '未分账流水') {
        query.fileName = '未分账流水.xlsx';
        query.type = '0';
    } else if (activeName == '押金流水') {
        query.fileName = '押金流水.xlsx';
        query.type = '2';
    } else if (activeName == '退款流水') {
        query.fileName = '退款流水.xlsx';
        query.type = '3';
    }
    const res = await service.fund.accountFundBalanceExport(query);
    res && common.export(res, query.fileName);
};
</script>
<template>
    <section class="main-white">
        <el-tabs v-model="activeName" type="card" class="demo-tabs" @tab-click="handleClick">
            <section class="box-wite">
                <div class="all-box" v-if="activeName == '已分账流水'">
                    <!-- 平台 -->
                    <div class="all-box-item" style="background: #fff1f1">
                        <div class="all-box-item-font">
                            <div class="all-box-item-font1">平台结算账户</div>
                            <div class="all-box-item-font2">¥{{ objectData.platBalanceAmount }}</div>
                        </div>
                        <div class="all-box-item-img" style="background: #ff554b">
                            <img src="../../assets/image/all.png" alt="" />
                        </div>
                    </div>
                </div>
                <section class="box-wite" v-if="activeName == '已分账流水'">
                    <div class="box-wite-title">查询汇总</div>
                    <!-- 平台 -->
                    <div class="box-wite-body">
                        <div class="box-wite-body-item">
                            <span>平台账户总收入：</span>
                            <span style="color: #e7584f">{{ objectData.platTotalAmount || 0 }}元</span>
                        </div>
                        <div class="box-wite-body-item">
                            <span>商家账户总分成：</span>
                            <span style="color: #e7584f">{{ objectData.merchantTotalAmount || 0 }}元</span>
                        </div>
                        <div class="box-wite-body-item">
                            <span>商家账户总提现：</span>
                            <span style="color: #e7584f">{{ objectData.merchantDrawAmount || 0 }}元</span>
                        </div>
                    </div>
                </section>
                <search ref="searchRef" :items="searchs" @filter="onSearch" @onLineNum="onLineNum" @export="exportData" :on-load="true" :key="updateSearchRefKey"/>
            </section>
            <el-tab-pane label="已分账流水" name="已分账流水"></el-tab-pane>
            <el-tab-pane label="未分账流水" name="未分账流水"></el-tab-pane>
            <el-tab-pane label="押金流水" name="押金流水"></el-tab-pane>
            <el-tab-pane label="退款流水" name="退款流水"></el-tab-pane>
        </el-tabs>

        <page-wrapper @get-data="getData" ref="pageRef" :on-load="true">
            <template v-slot:default="props">
                <vxe-table class="mytable-scrollbar" ref="tableRef" :loading="false" :data="props.tableData.records" :max-height="common.tableHeight(true, numLine)" highlight-hover-row round stripe border>
                    <vxe-column type="seq" width="50" fixed="left"></vxe-column>
                    <!-- v-if="activeName == '已分账流水'" -->
                    <vxe-column v-if="activeName == '已分账流水'" field="balanceAmount" title="金额（元）" width="120">
                        <template #default="{ row }">
                            <span :style="{ color: row.balanceType === 1 || row.balanceType === 3 ? 'green' : 'red' }">{{ row.balanceType === 1 || row.balanceType === 3 ? '+' : '-' }}{{ row.balanceAmount }}</span>
                        </template>
                    </vxe-column>
                    <vxe-column v-if="activeName == '押金流水'" field="balanceAmount" title="金额（元）" width="120">
                        <template #default="{ row }">
                            <span :style="{ color: row.balanceType === 1 || row.balanceType === 3 ? 'green' : 'red' }">{{ row.balanceType === 1 || row.balanceType === 3 ? '+' : '-' }}{{ row.balanceAmount }}</span>
                        </template>
                    </vxe-column>
                    <vxe-column v-if="activeName == '未分账流水' || activeName == '退款流水'" field="balanceAmount" title="金额（元）" width="120">
                        <template #default="{ row }">
                            <span :style="{ color: row.balanceType === 1 || row.balanceType == 3 ? 'green' : 'red' }">{{ row.balanceType === 1 || row.balanceType == 3 ? '+' : '-' }}{{ row.balanceAmount }}</span>
                        </template>
                    </vxe-column>
                    <vxe-column v-if="activeName != '未分账流水' && activeName != '退款流水'" field="afterBalanceAmount" title="交易后金额（元）" width="120" />
                    <vxe-column title="租金流向" width="120">
                        <template #default="{ row }">
                            <!-- <span :style="{ color: row.balanceType == 1 || row.balanceType == 4 ? 'green' : 'red' }">{{ row.balanceDirectionDesc }}</span> -->
                            <!-- <span v-else-if="activeName == '未分账流水' || activeName == '退款流水'" :style="{ color: row.inStatus === 1 || row.inStatus == 4 ? 'green' : 'red' }">{{ row.balanceDirectionDesc }}</span> -->
                            <!-- <span v-else-if="activeName != '以分账流水'">{{ row.balanceDirectionDesc }}</span> -->
                            <span :style="{ color: row.balanceType === 1 || row.balanceType === 3 ? 'green' : 'red' }">
                                <span v-if="row.balanceType <= 2">{{ row.balanceType == 1 ? '流入' : '流出' }}</span>
                                <span v-if="row.balanceType > 2">{{ row.balanceType == 3 ? '冻结' : '解冻' }}</span>
                            </span>
                        </template>
                    </vxe-column>
                    <vxe-column field="bizTypeDesc" title="交易类型" width="140"></vxe-column>
                    <vxe-column v-if="activeName == '押金流水'" field="balanceDirectionDesc" title="流向账户" width="140"></vxe-column>
                    <vxe-column v-if="activeName == '已分账流水' || activeName == '押金流水'" field="balanceNo" title="交易流水号" width="200"></vxe-column>
                    <vxe-column v-if="activeName == '未分账流水' || activeName == '退款流水'" field="outTradeNo" title="支付流水ID" width="160"></vxe-column>
                    <!-- <vxe-column v-if="activeName == '押金流水'" field="outTradeNo" title="流水号" width="160"></vxe-column> -->

                    <vxe-column field="createTime" title="支付时间" width="160"></vxe-column>
                    <vxe-column field="orderId" title="订单编号" width="160"></vxe-column>
                    <vxe-column field="goodsName" title="产品" width="200"></vxe-column>
                    <vxe-column field="companyName" title="商家名称" width="200"></vxe-column>
                    <!-- <vxe-column v-if="activeName == '押金流水'" field="companyName" title="商家名称" width="200"></vxe-column> -->
                    <!-- <vxe-column v-if="activeName != '已分账流水'" field="label" title="商家名称" width="160"></vxe-column> -->
                    <vxe-column field="splitNum" title="分期期数" width="90"></vxe-column>
                    <vxe-column field="marketPrice" title="签约价（元）" width="90"></vxe-column>
                    <vxe-column field="userName" title="用户姓名" width="90"></vxe-column>
                    <vxe-column title="订单状态" width="90">
                        <template #default="{ row }">
                            {{ common.getValueByKey('value', row.state, orderStateList, 'label') }}
                        </template>
                    </vxe-column>
                </vxe-table>
            </template>
        </page-wrapper>
    </section>
</template>
<style lang="scss" scoped>
:deep(.el-button + .el-button) {
    margin-left: 10px;
}

:deep(.is-active) {
    background: white;
}

.main-white {
    background: #f1f3f6;
    // margin: 0;
    // padding: 20px 24px;
    // box-sizing: border-box;
}

.main-all {
    display: flex;
    justify-content: space-between;
    background: #f5f5f5;
    background: #f1f3f6;
    margin-bottom: 20px;

    .main-all-item {
        background: #ffffff;
        width: 300px;
        height: 200px;
        border-radius: 8px;
        display: inline-flex;
        flex-direction: column;
        align-items: left;
        justify-content: center;
        padding-left: 40px;
    }
}

/*滚动条整体部分*/
.mytable-scrollbar ::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}

/*滚动条的轨道*/
.mytable-scrollbar ::-webkit-scrollbar-track {
    background-color: #ffffff;
}

/*滚动条里面的小方块，能向上向下移动*/
.mytable-scrollbar ::-webkit-scrollbar-thumb {
    background-color: #bfbfbf;
    border-radius: 5px;
    border: 1px solid #f1f1f1;
    box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
}

.mytable-scrollbar ::-webkit-scrollbar-thumb:hover {
    background-color: #a8a8a8;
}

.mytable-scrollbar ::-webkit-scrollbar-thumb:active {
    background-color: #787878;
}

/*边角，即两个滚动条的交汇处*/
.mytable-scrollbar ::-webkit-scrollbar-corner {
    background-color: #ffffff;
}

.box-wite {
    padding: 20px;
    margin: 0;
    background: #ffffff;
    box-sizing: border-box;
    border-radius: 6px;

    .box-wite-title {
        font-size: 18px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #1f1f20;
        border-bottom: 1px solid #e6e6e6;
        padding-bottom: 14px;
    }

    .box-wite-body {
        width: 100%;
        padding-top: 20px;
        display: flex;
        justify-content: space-between;

        .box-wite-body-item {
            box-sizing: border-box;
            width: 300px;
            height: 70px;
            background: #ffffff;
            border-radius: 4px;
            border: 1px solid #ffb3ae;
            display: flex;
            align-items: center;
            justify-content: center;
        }
    }

    .all-box {
        display: flex;
        // margin-bottom: 30px;

        .all-box-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 295px;
            height: 140px;
            background: #fff6de;
            border-radius: 4px;
            margin-right: 25px;
            padding: 0 20px 0 25px;

            .all-box-item-font {
                .all-box-item-font1 {
                    font-size: 14px;
                    font-family: PingFangSC-Regular, PingFang SC;
                    font-weight: 400;
                    color: #595959;
                    line-height: 20px;
                    margin-bottom: 22px;
                }

                .all-box-item-font2 {
                    font-size: 32px;
                    font-family: DINCondensed-Bold, DINCondensed;
                    font-weight: bold;
                    color: #1f1f20;
                }
            }

            .all-box-item-img {
                width: 74px;
                height: 74px;
                background: #ffde81;
                border-radius: 20px;
                display: flex;
                align-items: center;
                justify-content: center;

                img {
                    width: 42px;
                    height: 42px;
                    // background: #ffffff;
                }
            }
        }
    }
}
.demo-tabs {
    margin-bottom: 30px;
}
</style>
